//
//  HTPremiumScreen.swift
//  MagaCore
//
//  Created by MrMessy on 2023/4/29.
//

import SwiftUI

struct HTPremiumScreen: View {
    @EnvironmentObject private var var_store: HTAppStore
    @State var var_isPush: Bool = false
    @State var var_curSubscribe: HTSubscribe?

    var body: some View {
        VStack {
            HTNavigationBar {
                VStack(alignment: .leading, spacing: 0) {
                    ScrollView(showsIndicators: false) {
                        VStack(spacing: 0) {
                            if var_curSubscribe != nil {
                                Spacer().frame(height: 15)
                                HTPremiumPlanSection(var_curSubscribe: $var_curSubscribe)
                                Spacer().frame(height: 15)
                                HTPremiumFollowSection()
                            }
                            Spacer().frame(height: 25)
                            HTPremiumAccessSection()
                            /** 新版本注释了*/
//              Spacer().frame(height: 15)
//              HTPremiumPackageSection()

                            Divider()
                                .padding(.top, 30)
                                .padding(.bottom, 20)
                            HTPremiumCard()
                            /** 注释了*/
//              HTPremiumTipSection()
                        }
                        .padding(.horizontal, 15)
                    }
                    .frame(maxWidth: .infinity)

                    Spacer()

                    HTPremiumPaySection()
                }
                .background(Color(hex: "#f5f5f5"))
                .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .topLeading)
                .padding(.bottom, var_isPush ? UIDevice.ht_tabBarHeight() : UIDevice.ht_tabBarFullHeight())
            }
            .ht_title(title: "Premium")
            .ht_backButtonHidden(var_isPush ? false : true)
        }
        .frame(maxWidth: .infinity, maxHeight: .infinity)
        .background(Color.white)
        .ignoresSafeArea()
        .onAppear {
            var_curSubscribe = var_store.state.var_subscribeState.var_curSubscribe ?? nil
            HTNetworkManager().ht_requestForBuried(.htEnumVipSh(var_source: "7"))
            HTNetworkManager().ht_request(.htEnumSubscribe(var_uid: "0", var_p1: "0")) { response in
                // 将字典转换为 JSON 字符串
                if let jsonData = try? JSONSerialization.data(withJSONObject:  response.ht_dataDist() as Any, options: []),
                   let jsonString = String(data: jsonData, encoding: .utf8) {
                    // 使用 HandyJSON 解析 JSON 字符串为模型
                    if let premiumModel = HTPremiumModel.deserialize(from: jsonString) {
                    }
                }
            }
        }
        .onReceive(var_store.objectWillChange) { _ in
            var_curSubscribe = var_store.state.var_subscribeState.var_curSubscribe ?? nil
        }
    }
}
